<include file="Public/head"/>
<include file="Public/common_header"/>
<script src="http://img1.isou365.cn/paimai/yunying/js/global.js"></script>
<div class="mian">
    <include file="Public/sidebar" menu="xtgl.cxkgl"/>
    <div class="mianR">
        <h3 class="yonghu"><if condition="$_GET['id'] gt 0">修改车型<else/>添加车型</if></h3>

        <form method="post" class="form-horizontal" style="padding-top:10px;">

        <div class="form-item" style="width:235px;float:left;">
            <label class="item-label">主品牌首字母<span class="check-tips"></span></label>

            <div class="controls">
                <select onchange="getMaster();" id="first_char">
                    <option value="">请选择</option>
                    <volist name="first" id="v">
                        <option value="{$v}">{$v}</option>
                    </volist>
                </select>
            </div>
        </div>

        <div class="form-item" style="width:235px;float:left;">
            <label class="item-label">选择主品牌</label>
            <div class="controls">
                <select id="master" onchange="getBrand();"> </select>
            </div>
        </div>
        <div class="form-item" style="width:235px;float:left;">
            <label class="item-label">选择品牌</label>
            <div class="controls">
                <select id="brand" onchange="getSeries();"> </select>
            </div>
        </div>
        <div class="form-item" style="width:235px;float:left;">
            <label class="item-label">选择车系</label>
            <div class="controls">
                <select id="series"></select>
            </div>
        </div>
        <div class="form-item" style="width:235px;float:left;">
            <label class="item-label">车型名称</label>
            <div class="controls">
                <input type="text" class="text input-large" id="name" value="{$detail.name}">
                <input type="hidden" id="old_name" value="{$detail.name}">
            </div>
        </div>
        <div class="form-item" style="width:235px;float:left;">
            <label class="item-label">生产状态</label>
            <div class="controls">
                <select id="produce_status">
                    <php>
                        foreach(C('CAR_MODEL_PRODUCE_STATUS') as $k=>$v){
                            echo '<option value="'.$k.'">'.$v.'</option>';
                        }
                    </php>
                </select>
            </div>
        </div>
        <div class="form-item" style="width:235px;float:left;">
            <label class="item-label">销售状态</label>
            <div class="controls">
                <select id="sale_status">
                    <php>
                        foreach(C('CAR_MODEL_SALE_STATUS') as $k=>$v){
                            echo '<option value="'.$k.'">'.$v.'</option>';
                        }
                    </php>
                </select>
            </div>
        </div>
        <div class="form-item" style="width:235px;float:left;">
            <label class="item-label">增压类型</label>
            <div class="controls">
                <select id="air_inlet_form">
                    <php>
                        foreach(C('CAR_MODEL_AIR_INLET_FORM') as $k=>$v){
                            echo '<option value="'.$v.'">'.$v.'</option>';
                        }
                    </php>
                </select>
            </div>
        </div>
        <div class="form-item" style="width:235px;float:left;">
            <label class="item-label">变速箱类型</label>
            <div class="controls">
                <select id="gearbox">
                    <php>
                        foreach(C('GEARBOX') as $k=>$v){
                            echo '<option value="'.$k.'">'.$v.'</option>';
                        }
                    </php>
                </select>
            </div>
        </div>
        <div class="form-item" style="width:235px;float:left;">
            <label class="item-label">年款</label>
            <div class="controls">
                <input type="text" class="text input-large" id="year" value="{$detail.year}">
            </div>
        </div>

        <div class="form-item" style="width:235px;float:left;">
            <label class="item-label">档位个数</label>
            <div class="controls">
                <input type="text" class="text input-large" id="gear" value="{$detail.gear}">
            </div>
        </div>
        <div class="form-item" style="width:235px;float:left;">
            <label class="item-label">排量(L)</label>
            <div class="controls">
                <input type="text" class="text input-large" id="car_cc" value="{$detail.car_cc}">
            </div>
        </div>
        <div class="form-item" style="width:245px;float:left;">
            <label class="item-label">油耗</label>
            <div class="controls">
                <input type="text" class="text input-large" id="consumption" value="{$detail.consumption}">
            </div>
        </div>
        <div class="form-item" style="width:265px;float:left;">
            <label class="item-label" style="width:100px">厂商指导价(万元)</label>
            <div class="controls">
                <input type="text" class="text input-large" id="refer_price" value="{$detail.refer_price}">
            </div>
        </div>
        <div class="clear"></div>
        <div class="data-table table-striped">
            <table>
                <thead>
                <tr>
                    <th>配置项</th>
                    <th>配置值</th>
                    <th>配置项</th>
                    <th>配置值</th>
                </tr>
                </thead>
                <tbody id="params">
                <php>$ctr='<tr>';foreach($patams AS $cname => $val):</php>
                <php>if($ctr == '<tr>'):echo $ctr;endif;</php>
                    <td width="220px;">{$cname}</td>
                    <td>
                        <php>if(is_array($val)):</php>
                        <select name="{$cname}">
                            <volist name="val" id="v">
                                <option value="{$v}" <php>if($config[$cname] == $v)echo 'selected="selected"';</php>>{$v}</option>
                            </volist>
                        </select>(推荐值：<php>echo $config[$cname];</php>)
                        <php>elseif($val == 'text'):</php>
                            <input type="text" class="text input-large" name="{$cname}" value="{$config.$cname}" style="width:180px;border:1px solid #4cdb00;"/>
                        <php>else:</php>
                            <label><input type="radio" name="{$cname}" value="0" checked="1"/>无</label>
                            <label><input type="radio" name="{$cname}" value="1" <php>if($config[$cname]) echo 'checked="1"';</php>/>有</label>
                        <php>endif;</php>
                    </td>
                <php>if($ctr == '</tr>'):echo $ctr;$ctr='<tr>';else:$ctr = '</tr>';endif;</php>
                <php>endforeach;</php>
                </tbody>
            </table>
        </div>
        <div class="form-item" style="height:50px;width:450px;">
            <input type="hidden" id="model_id" value="{$detail.model_id}">
            <button class="btn submit-btn ajax-post" type="button" style="line-height:20px">确 定</button>
            <button class="btn btn-return" onclick="self.close()" style="line-height:18px">关  闭</button>
        </div>
        </form>
    </div>
</div>
<div class="clear"></div>
<script>
    var model_id = $('#model_id').val();
    if(model_id>0){
        $('#first_char').val('{$detail.first_char}');

        $.ajaxSetup({async:false});
        getMaster();
        $('#master').val('{$detail.mbrand_id}');
        getBrand();
        $('#brand').val('{$detail.brand_id}');
        getSeries();
        $('#series').val('{$detail.series_id}');
        $.ajaxSetup({async:true});

        $('#produce_status').val('{$detail.produce_status}');
        $('#sale_status').val('{$detail.sale_status}');
        $('#air_inlet_form').val('{$detail.air_inlet_form}');
        $('#gearbox').val('{$detail.gearbox}')
    }
    $('.ajax-post').on('click',function(){
        var series_id = $('#series').val();

        var name = $('#name').val();
        var old_name = $('#old_name').val();

        var produce_status = $('#produce_status').val();
        var sale_status = $('#sale_status').val();
        var air_inlet_form = $('#air_inlet_form').val();
        var gearbox = $('#gearbox').val();
        var year = $('#year').val();
        var gear = $('#gear').val();
        var car_cc = $('#car_cc').val();
        var consumption = $('#consumption').val();
        var refer_price = $('#refer_price').val();

        var model_id = $('#model_id').val();

        if(series_id<1){
           shibai_alert('请选择车系');return;
        }
        if($.trim(name)==''){
           shibai_alert('请填写车型名称');return;
        }

        if(year==''){
           shibai_alert('请填写出厂年份');return;
        }
        if(gear==''){
           shibai_alert('请填写档位数');return;
        }
        if(car_cc==''){
           shibai_alert('请填写排量');return;
        }
//        if(consumption==''){
//           shibai_alert('请填写油耗');return;
//        }
//        if(refer_price==''){
//           shibai_alert('请填写厂商指导价');return;
//        }

        var paramer = {
            "series_id":series_id,

            "name":encodeURIComponent(name),
            "old_name":encodeURIComponent(old_name),

            "produce_status":produce_status,
            "sale_status":sale_status,

            "air_inlet_form":air_inlet_form,

            "gearbox": gearbox,
            "year": year,
            "gear":gear,
            "car_cc":car_cc,
            "consumption":consumption,
            "refer_price":refer_price,

            "detail_params": $('.form-horizontal').serialize(),

            "model_id":model_id
        };

        $.post("/CarModel/model_add/",paramer, function (data) {
            if(data.code=='200'){
                btn_alert(data.msg,2,function(){window.location.href='/CarModel/model_add/id/'+(model_id?model_id:data.model_id)});
            }else{
                shibai_alert(data.msg)
            }
        }, "json");
    })
    //异步生成主品牌下拉菜单
    function getMaster(){
        var curr_char = $('#first_char').val();
        var mbrand_id = $('#master').val();
        var paramer = {
            "type": 'mbrand',
            "char":curr_char,
            "json":1,
            "rnd":Math.random()
        };

        $("#master option").remove();
        $.get("/CarModel/get_list/",paramer, function (data) {
            var option = ("<option value='0' >请选择主品牌</option>");
            if(data.data[0] !== null && data.data[0].length>0){
                for(var i=0;i<data.data[0].length;i++){
                    option += ("<option value='"+data.data[0][i].mbrand_id+"' "+(mbrand_id==data.data[0][i].mbrand_id ? 'selected' :'')+" >"+data.data[0][i].name+"</option>")
                }
            }else{
                option += ("<option value='0' >无</option>")
            }
            $("#master").append(option);
        }, "json");
    }

    //初始化品牌下拉菜单
    function getBrand(){
        var curr_char = $('#first_char').val();
        var mbrand_id = $("#master").val();
        var brand_id = $("#brand").val();
        var paramer = {
            "type": 'brand',
            "char": curr_char,
            "mbrand_id":mbrand_id,
            "rnd":Math.random()
        };
        $.get("/CarModel/get_list/json/1/",paramer, function (data) {
            $("#brand option").remove();
            var option = ("<option value='0' >请选择品牌</option>")
            if(data.data[0] !== null && data.data[0].length>0){
                for(var i=0;i<data.data[0].length;i++){
                    option += ("<option value='"+data.data[0][i].brand_id+"' "+(brand_id==data.data[0][i].brand_id ? 'selected' :'')+">"+data.data[0][i].name+"</option>")
                }
            }else{
                option += ("<option value='0'>无</option>")
            }
            $("#brand").append(option)
        }, "json");

    }
    //初始化车系下拉菜单
    function getSeries(){
        var brand_id  = $('#brand').val();
        var series_id  = $('#series').val();
        var paramer = {
            "p": 1,
            "type": 'series',
            "brand_id":brand_id
        };

        $.get("/CarModel/get_list/json/1/",paramer, function (data) {
            $("#series option").remove();
            var option = ("<option value='0' >请选择车系</option>")
            if(data.data[0] !== null && data.data[0].length>0){
                for(var i=0;i<data.data[0].length;i++){
                    option += ("<option value='"+data.data[0][i].series_id+"' "+(series_id==data.data[0][i].series_id ? 'selected' :'')+">"+data.data[0][i].name+"</option>")
                }
            }else{
                option += ("<option value='0'>无</option>")
            }
            $("#series").append(option);
        }, "json");
    }

</script>
<include file="Public/foot"/>